package com.inspur.service.impl;

import com.inspur.common.ResultCode;
import com.inspur.common.ServerResponse;
import com.inspur.dao.UserAccountMapper;
import com.inspur.dao.UserInfoMapper;
import com.inspur.pojo.UserAccount;
import com.inspur.pojo.UserInfo;
import com.inspur.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class IUserServiceImpl implements IUserService {

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public ServerResponse login(String userName, String password) {
        UserAccount userAccount = userAccountMapper.getUserAccountByUserName(userName);
        if(userAccount==null){
            return ServerResponse.getResponseFail(ResultCode.USER_NOT_EXIST);
        }
        if(password.equals(userAccount.getPassword())){
            userAccount.setPassword("********");
            return ServerResponse.getSuccessByData(userAccount);
        }
        return ServerResponse.getResponseFail(ResultCode.PASSWORD_ERROR);
    }

    @Override
    public ServerResponse register(UserAccount userAccount, UserInfo userInfo) {
        int i =userAccountMapper.insertSelective(userAccount);
        userInfo.setCreateTime(new Date());
        userInfo.setUpdateTime(new Date());
        int y =userInfoMapper.insertSelective(userInfo);
        if(i==1 && y==1){
            System.out.println("成功");
        }
        return ServerResponse.getSuccess();
    }

    public ServerResponse getUserInfoByUserName(String userName){
        UserInfo userInfo =userInfoMapper.getUserInfoByUserName(userName);
        return ServerResponse.getSuccessByData(userInfo);
    }
}
